package com.wanxi.dao.Impl;

import com.wanxi.dao.NavDao;
import com.wanxi.model.NavModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Li
 * @Date 2023/5/7 13:34
 */
public class NavDaoImpl implements NavDao {
    public int add(NavModel navModel) {
        String sql = "insert into navs (`nav_name`,`nav_href`,`create_time`,`update_time`,`enable`,`remark`)" +
                " value('"+navModel.getNavName()+"','"+navModel.getNavHref()+"',now(),sysdate(),'"+navModel.getEnable()+"','"+navModel.getRemark()+"');";
        return JDBC.update(sql);
    }

    public int update(NavModel navModel) {
        String sql = "update navs set `nav_name` = '"+navModel.getNavName()+"', `nav_href` = '"+navModel.getNavHref()+"',`update_time` = now()," +
                " `enable` = '"+navModel.getEnable()+"',`remark` = '"+navModel.getRemark()+"' where (`id` = '"+navModel.getId()+"') ";
        return JDBC.update(sql);
    }

    public int delete(NavModel navModel) {
        String sql = "delete from navs where id = '"+navModel.getId()+"' ";
        return JDBC.update(sql);
    }

    public List<NavModel> findAll(NavModel navModel) {
        int start = (navModel.getPage()-1)*navModel.getLimit();
        String sql = "select * from navs where true";
        sql += getWhereSql(navModel);
        sql += " order by id desc";
        sql += " limit "+start+","+navModel.getLimit()+" ";
        ResultSet query = JDBC.query(sql);
        List<NavModel> navModelList = new ArrayList<NavModel>();
        try{
            while (query.next()){
                navModelList.add(NavModel.builder()
                        .id(query.getInt("id"))
                        .navName(query.getString("nav_name"))
                        .navHref(query.getString("nav_href"))
                        .remark(query.getString("remark"))
                        .enable(query.getInt("enable"))
                        .build());
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return navModelList;
    }

    private String getWhereSql(NavModel navModel){
        String sql = "";
        if (!"".equals(navModel.getNavName())){
            sql += " and nav_name like '%"+navModel.getNavName()+"%' ";
        }if (navModel.getEnable()!=-1){
            sql +=" and enable = '"+navModel.getEnable()+"' ";
        }
        return sql;
    }

    public NavModel findById(NavModel navModel) {
        String sql = "select * from navs where id = '"+navModel.getId()+"'";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
            return NavModel.builder()
                    .id(query.getInt("id"))
                    .navName(query.getString("nav_name"))
                    .navHref(query.getString("nav_href"))
                    .enable(query.getInt("enable"))
                    .remark(query.getString("remark"))
                    .build();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public Integer count(NavModel navModel) {
        String sql = "select count(*) as count from navs where true";
     sql += getWhereSql(navModel);
        ResultSet query = JDBC.query(sql);
        return JDBC.count(sql);
    }

    @Override
    public Integer totalCount() {
        return null;
    }

    public boolean findSameName(NavModel navModel) {
        String sql = "select * from navs where nav_name = '"+navModel.getNavName()+"' ";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
}
